package org.keyroy.map_editor.map.m;

import java.awt.Graphics;

import org.keyroy.tag.Tag;

public class Rect extends Point implements DrawableShape {
	public int width;
	public int height;

	public Rect() {
	}

	public Rect(int width, int height) {
		setSize(width, height);
	}

	public final void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public final void setSize(Rect rect) {
		this.width = rect.width;
		this.height = rect.height;
	}

	public final void setRight(int right) {
		this.width = right - x;
	}

	public final void setBottom(int bottom) {
		this.height = bottom - y;
	}

	public void setBounds(int right, int bottom) {
		setSize(right - x, bottom - y);
	}

	public void set(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.width = w;
		this.height = h;
	}

	public void set(Rect rect) {
		this.x = rect.x;
		this.y = rect.y;
		this.width = rect.width;
		this.height = rect.height;
	}

	public final int getRight() {
		return x + width;
	}

	public final int getBottom() {
		return y + height;
	}

	@Override
	public String getTagName() {
		return "r";
	}

	@Override
	public void initObj(Tag tag) {
		super.initObj(tag);
		width = tag.getParameterIntValue("w");
		height = tag.getParameterIntValue("h");
	}

	@Override
	public void initTag(Tag tag) {
		super.initTag(tag);
		tag.addParameter("w", width, width != 0);
		tag.addParameter("h", height, height != 0);
	}

	public final boolean isIntersectingRect(Rect rect) {
		return isIntersectingRect(x, y, width, height, rect.x, rect.y, rect.width, rect.height);
	}

	public final boolean isIntersectingRect(Point p) {
		return isIntersectingRect(x, y, width, height, p.x, p.y, 0, 0);
	}

	protected static final boolean isIntersectingRect(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) {
		if (by + bh < ay || // is the bottom of b above the top of a?
				by > ay + ah || // is the top of b below bottom of a?
				bx + bw < ax || // is the right of b to the left of a?
				bx > ax + aw) // is the left of b to the right of a?
			return false;

		return true;
	}

	public static final void reset(Rect in, Rect out) {
		int x, y, r, b;
		if (in.x < in.getRight()) {
			x = in.x;
			r = in.getRight();
		} else {
			x = in.getRight();
			r = in.x;
		}

		if (in.y < in.getBottom()) {
			y = in.y;
			b = in.getBottom();
		} else {
			y = in.getBottom();
			b = in.y;
		}

		out.setLocation(x, y);
		out.setBounds(r, b);
	}

	public void reset() {
		if (width < 0) {
			x = x + width;
			width = -width;
		}

		if (height < 0) {
			y = y + height;
			height = -height;
		}
	}

	@Override
	public void drawShape(Graphics g) {
		g.drawRect(x, y, width - 1, height - 1);
	}

	@Override
	public void fillShape(Graphics g) {
		g.fillRect(x, y, width, height);
	}

	@Override
	public String toString() {
		return "[x = " + x + "    y = " + y + "    w = " + width + "    h = " + height + "]";
	}

	public static boolean isInsideTriangle(int cx, int cy, int[] x, int[] y) {
		float vx2 = cx - x[0];
		float vy2 = cy - y[0];
		float vx1 = x[1] - x[0];
		float vy1 = y[1] - y[0];
		float vx0 = x[2] - x[0];
		float vy0 = y[2] - y[0];
		float dot00 = vx0 * vx0 + vy0 * vy0;
		float dot01 = vx0 * vx1 + vy0 * vy1;
		float dot02 = vx0 * vx2 + vy0 * vy2;
		float dot11 = vx1 * vx1 + vy1 * vy1;
		float dot12 = vx1 * vx2 + vy1 * vy2;
		float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
		float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
		if (u <= 0) {
			return false;
		} else {
			float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
			if (v <= 0) {
				return false;
			} else {
				return (u + v < 1);
			}
		}
	}
}
